home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / APW.SC / SC17Darts / Darts.c / UWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1990-06-24  |  10.4 KB  |  391 lines  |  [TEXT/pdos]

  1. /***********************************************************************
  2. *
  3. * darts uwindow.c -- Version 3.0 
  4. *
  5. * Copyright (c)
  6. * Apple Computer, Inc.  1986-1990
  7. * All Rights Reserved.
  8. *
  9. * Developer Technical Support Apple II Sample Code
  10. *
  11. * This file contains the code which implements  
  12. * windows in the program.
  13. *
  14. ***********************************************************************/
  15.  
  16. #include <types.h>
  17. #include <control.h>
  18. #include <intmath.h>
  19. #include <list.h>
  20. #include <memory.h>
  21. #include <resources.h>
  22. #include <window.h>
  23. #include "darts.h"
  24.  
  25. extern GrafPortPtr      theWindow;
  26. extern unsigned int     firstUpdateComplete, gameType, weHaveAWinner, userID;
  27. extern unsigned int     crickettTables[NumPlayers][26], score[NumPlayers];
  28. extern unsigned int     listEntries[NumPlayers];
  29. extern char             *scoreStringArray[NumPlayers];
  30. extern char             scoreStrings[NumPlayers][64];
  31. extern MyMemRecHndl     scoreList[NumPlayers];
  32.  
  33. extern char     test[80];
  34.  
  35.  
  36.  
  37. /***********************************************************************
  38. *
  39. * invalScore
  40. *
  41. * Invalidates a rectangle that holds the score for player one or two
  42. * so that it will be updated.
  43. *
  44. ***********************************************************************/
  45. void            invalScore (playerNum)
  46. unsigned int    playerNum;
  47. {
  48.     CtlRecHndl      tempHndl;
  49.     unsigned long   id;
  50.     GrafPortPtr     keepPort;
  51.  
  52.     id = 0x60000L;
  53.     if (!playerNum) id = 0x50000L;
  54.  
  55.     tempHndl = GetCtlHandleFromID(theWindow, id);
  56.  
  57.     keepPort = GetPort();
  58.     SetPort(theWindow);
  59.     InvalRect(&(*tempHndl)->ctlRect);
  60.     SetPort(keepPort);
  61. }
  62.  
  63.  
  64.  
  65. void            int2pstr(val, str)
  66. unsigned int    val;
  67. char            *str;
  68. {
  69.     unsigned int    j, k;
  70.  
  71.     Int2Dec(val, str + 1, k = 6, 0);
  72.  
  73.     while (str[1] == 32) {
  74.         for (j = 1; j < k; j++) str[j] = str[j + 1];
  75.         k--;
  76.     }
  77.     *str = k;
  78. }
  79.  
  80.  
  81.  
  82. /***********************************************************************
  83. *
  84. * drawThisWindow
  85. *
  86. * Draws the window whose port is the current port.
  87. *
  88. ***********************************************************************/
  89. void    drawThisWindow()
  90. {
  91.     unsigned int    dbreg, i;
  92.     Rect            tempRect;
  93.  
  94.     dbreg = SaveDB();
  95.  
  96.     firstUpdateComplete = 1;
  97.  
  98.     if (!gameType) {
  99.         GetPortRect(&tempRect);
  100.         tempRect.h1 += ((tempRect.h2 - tempRect.h1) >> 1);
  101.         tempRect.h2 = tempRect.h1 + 2;
  102.         tempRect.v1 += 15;
  103.         tempRect.v2 -= 18;
  104.         PaintRect(&tempRect);
  105.     }
  106.  
  107.     for (i = 0; i < NumPlayers; i++) {
  108.         int2pstr(score[i], scoreStrings[i]);
  109.     }
  110.  
  111.     SetCtlParamPtr(scoreStringArray);
  112.  
  113.     DrawControls(theWindow);
  114.     RestoreDB(dbreg);
  115. }
  116.  
  117.  
  118.  
  119. /***********************************************************************
  120. *
  121. * drawListItem
  122. *
  123. * Draws the indicated list item.  
  124. *
  125. ***********************************************************************/
  126. pascal void     drawListItem(rptr, memRecPtr, listCtl)
  127. Rect            *rptr;
  128. MyMemRecPtr     memRecPtr;
  129. Handle          listCtl;
  130. {
  131.     unsigned int    dbreg, theScore, selected, mode;
  132.     char            str[10], *cptr;
  133.  
  134.     dbreg = SaveDB();
  135.  
  136.     theScore = (int)memRecPtr->memPtr;
  137.     selected = ((memRecPtr->memFlag & 0xC0) == 0x80);
  138.  
  139.     mode = GetPenMode();
  140.     if (!selected) SetPenMode(0x8000);
  141.     PaintRect(rptr);
  142.     SetPenMode(mode);
  143.  
  144.     int2pstr(theScore, cptr = str);     /* Assigning cptr to str saves code due to
  145.                                         ** the below reference to cptr.
  146.                                         */
  147.  
  148.     MoveTo(rptr->h1 + 4, rptr->v2 - 1);
  149.  
  150.     mode = GetTextMode();
  151.     SetTextMode(0x8002);
  152.     DrawString(cptr);
  153.     SetTextMode(mode);
  154.  
  155.     RestoreDB(dbreg);
  156. }
  157.  
  158.  
  159.  
  160. /***********************************************************************
  161. *
  162. * addToList
  163. *
  164. * Adds the indicated score to the right list.  
  165. *
  166. ***********************************************************************/
  167. void            addToList(playerNum, amount)
  168. unsigned int    playerNum, amount;
  169. {
  170.     unsigned int    topOfList;
  171.     ListCtlRecHndl  theHndl;
  172.     MyMemRecPtr     memRecPtr;
  173.     unsigned long   size, id;
  174.  
  175.     listEntries[playerNum]++;
  176.     topOfList = listEntries[playerNum] - 3;
  177.     if ((!topOfList) || (topOfList & 0x8000)) topOfList = 1;
  178.  
  179.     if (
  180.         (size = listEntries[playerNum] * sizeof(MyMemRec)) >=
  181.         GetHandleSize(scoreList[playerNum])
  182.     ) {
  183.         SetHandleSize(size + 100, scoreList[playerNum]);
  184.     }
  185.  
  186.     memRecPtr = (*scoreList[playerNum]) + (listEntries[playerNum] - 1);
  187.     memRecPtr->memPtr = (char *)amount;
  188.     memRecPtr->memFlag = 0;
  189.     
  190.     id = 0x40000L;
  191.     if (!playerNum) id = 0x30000L;
  192.     theHndl = GetCtlHandleFromID(theWindow, id);
  193.  
  194.     NewList2(
  195.         drawListItem,                           /* drawing routine addr */
  196.         topOfList,                              /* First Item to display */
  197.         scoreList[playerNum],                   /* ref to mem rec array */
  198.         refIsHandle,                            /* List type */
  199.         listEntries[playerNum],                 /* Num Items */
  200.         theHndl                                 /* List ctl handle */
  201.     );
  202. }
  203.  
  204.  
  205.  
  206. /*****************************************************************************
  207. *
  208. * fixButtonTitle
  209. *
  210. * This routine is called only when the chosen game is crickett.  Its job
  211. * is to adjust the button title of the button associated with the amount
  212. * passed.
  213. *
  214. * There are four possible states of the button title.  Which state it is
  215. * in depends on the number times it has been pressed (contained in the
  216. * CrickettTables).
  217. *
  218. *****************************************************************************/
  219. void            fixButtonTitle(playerNum, amount)
  220. unsigned int    playerNum, amount;
  221. {
  222.     CtlRecHndl      theHndl;
  223.     unsigned int    num;
  224.     long            theID;
  225.     char            *cptr;
  226.  
  227.     if (playerNum == Player1) theID = amount;
  228.     else theID = amount | 0x8000;
  229.  
  230.     theHndl = GetCtlHandleFromID(theWindow, theID);
  231.     switch(num = crickettTables[playerNum][amount]) {
  232.         case 0:
  233.             cptr = "\p ";
  234.             break;
  235.         case 1:
  236.             cptr = "\p/";
  237.             break;
  238.         case 2:
  239.             cptr = "\pX";
  240.             break;
  241.         case 3:
  242.             cptr = "\p*";
  243.             break;
  244.     }
  245.     if (num < 4) SetCtlTitle(cptr, theHndl);
  246. }   
  247.  
  248.  
  249.  
  250. /***********************************************************************
  251. *
  252. * removeSelected
  253. *
  254. * Removes any selected scores from the list.
  255. *
  256. ***********************************************************************/
  257. void            removeSelected(playerNum)
  258. unsigned int    playerNum;
  259. {
  260.     unsigned int    itemToRemove, amount, topOfList;
  261.     ListCtlRecHndl  theHndl;
  262.     MyMemRecPtr     memRecPtr;
  263.     unsigned long   id, size;
  264.  
  265.     id = 0x40000L;
  266.     if (!playerNum) id = 0x30000L;
  267.     theHndl = GetCtlHandleFromID(theWindow, id);
  268.  
  269.     for (;;) {
  270.         itemToRemove = NextMember2(0, theHndl);
  271.         if (itemToRemove) {
  272.             amount = (unsigned int)(*scoreList[playerNum])[itemToRemove - 1].memPtr;
  273.             if (!gameType) score[playerNum] -= amount;
  274.             else {
  275.                 if (crickettTables[playerNum][amount] > 3) {
  276.                     score[playerNum] -= amount;
  277.                 }
  278.                 crickettTables[playerNum][amount]--;
  279.                 fixButtonTitle(playerNum, amount);
  280.             }
  281.  
  282.             memRecPtr = (*scoreList[playerNum]) + (itemToRemove - 1);
  283.             size = (listEntries[playerNum] - itemToRemove) * sizeof(MyMemRec);
  284.             BlockMove(memRecPtr + 1, memRecPtr, size);
  285.  
  286.             listEntries[playerNum]--;
  287.             topOfList = GetCtlValue(theHndl) - 1;
  288.             if (!topOfList) topOfList++;
  289.  
  290.             NewList2(
  291.                 drawListItem,                           /* drawing routine addr */
  292.                 topOfList,                              /* First Item to display */
  293.                 scoreList[playerNum],                   /* ref to mem rec array */
  294.                 refIsHandle,                            /* List type */
  295.                 listEntries[playerNum],                 /* Num Items */
  296.                 theHndl                                 /* List ctl handle */
  297.             );
  298.         }
  299.         else break;
  300.     }
  301.     invalScore(playerNum);
  302. }
  303.  
  304.  
  305.  
  306. /***********************************************************************
  307. *
  308. * clearList
  309. *
  310. * Clears the indicated list.  
  311. *
  312. ***********************************************************************/
  313. void            clearList(playerNum)
  314. unsigned int    playerNum;
  315. {
  316.     ListCtlRecHndl  theHndl;
  317.     unsigned long   id;
  318.  
  319.     id = 0x30000L;
  320.     if (!playerNum) id = 0x40000L;
  321.     theHndl = GetCtlHandleFromID(theWindow, id);
  322.  
  323.     listEntries[playerNum] = 0;
  324.     NewList2(
  325.         drawListItem,                           /* drawing routine addr */
  326.         0,                                      /* Item to display */
  327.         scoreList[playerNum],                   /* ref to mem rec array */
  328.         refIsHandle,                            /* List type */
  329.         0,                                      /* Num Items */
  330.         theHndl                                 /* List ctl handle */
  331.     );
  332. }
  333.  
  334.  
  335.  
  336. /***********************************************************************************
  337. *
  338. * startUpRobinGame
  339. *
  340. * Opens the robin game window and zeros the scores.
  341. *
  342. ***********************************************************************************/
  343. void    startupRobinGame()
  344. {
  345.     theWindow = NewWindow2
  346.         (NULL, NULL, drawThisWindow, NULL, refIsResource, RobinWindow, rWindParam1);
  347.  
  348.     listEntries[Player1] = listEntries[Player2] = 0;
  349. }
  350.  
  351.  
  352.  
  353. /************************************************************************************
  354. *
  355. * startupCrickettGame
  356. *
  357. * Opens the crickett game window and zeros the scores.
  358. *
  359. ************************************************************************************/
  360. void    startupCrickettGame()
  361. {
  362.     theWindow = NewWindow2
  363.         (NULL, 1L, drawThisWindow, NULL, refIsResource, CrickettWindow, rWindParam1);
  364.  
  365.     listEntries[Player1] = listEntries[Player2] = weHaveAWinner = 0;
  366. }
  367.  
  368.  
  369.  
  370. /***********************************************************************
  371. *
  372. * setupWindows
  373. *
  374. * Inits any of the window unit variables we need to start up 
  375. * the game.  Calls the routine that opens the first window.
  376. *
  377. ***********************************************************************/
  378. void    setupWindows()
  379. {
  380.     unsigned int    i;
  381.  
  382.     firstUpdateComplete = 0;
  383.     for (i = 0; i < NumPlayers; i++) {
  384.         scoreList[i] =
  385.             (MyMemRecHndl)NewHandle(sizeof(MyMemRec) * 100L, userID, 0, NULL);
  386.     }
  387.     listEntries[Player1] = listEntries[Player2] = 0;
  388.  
  389.     startupRobinGame();
  390. }
  391.